feat(clerk-js): experimental SWR initialization for faster page loads#8153
feat(clerk-js): experimental SWR initialization for faster page loads#8153nikosdouvlis wants to merge 1 commit into
Conversation
…oads Clerk initialization blocks on /client (slow server roundtrip). This adds an opt-in experimental.swr flag that lets clerk-js initialize immediately from a cached client snapshot, validated by a fast token check, then silently swap to fresh /client data in the background. The flow: read cached client from localStorage, check if the __session cookie JWT is still valid (SSR apps always have one from middleware refresh). If valid, emit ready instantly with zero network. If expired, call getToken (edge-routed, fast) to validate session liveness. If revoked (4xx), discard cache and fall through to normal flow. If transient error, emit degraded with cached data. Cache is scoped by publishable key, versioned for schema safety, strips JWTs and signIn/signUp state before saving, and clears on sign-out (including cross-tab via BroadcastChannel).
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Hello 👋 We currently close PRs after 60 days of inactivity. It's been 50 days since the last update here. If we missed this PR, please reply here. Otherwise, we'll close this PR in 10 days. Thanks for being a part of the Clerk community! 🙏 |
Why
Clerk initialization blocks on the /client API response, which is a full server roundtrip (500ms-2s). For returning users, this means a loading spinner on every page load even though we already have their session data from the previous visit. Auth providers are infrastructure, and infrastructure that initializes faster wins.
What
Adds an opt-in
experimental.swrflag toClerk.load()that enables stale-while-revalidate initialization. When enabled, clerk-js:__sessioncookie JWT is still validgetToken()(edge-routed, fast) to validate session livenessThe cache is scoped by publishable key, versioned for schema safety, strips JWTs and signIn/signUp flow state before saving, and clears on sign-out (including cross-tab via BroadcastChannel).
Key design decisions
Test plan
experimental: { swr: true }, sign in, reload, verify instant load🤖 Generated with Claude Code